﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace com.dz.ct.common
{
    public class TxtChapterManager
    {
        /// <summary>
        /// 是否打开文件
        /// </summary>
        private bool IsOpenFile = false;
        /// <summary>
        /// 文件流
        /// </summary>
        private FileStream fileStream = null;
        /// <summary>
        /// 文件写入器
        /// </summary>
        private StreamWriter writer = null;
        /// <summary>
        /// 旧文件名
        /// </summary>
        private string oldFileName = "";
        /// <summary>
        /// 锁，防止多线程破坏文件
        /// </summary>
        private static object syncRoot = new object();
        /// <summary>
        /// 时间戳
        /// </summary>
        private DateTime lastFlushTime = DateTime.Now;

        private string _logPath;
        /// <summary>
        /// 文件路径
        /// </summary>
        public string LogPath
        {
            get
            {
                return AppConfig.BookChapterRoot;
            }
            set
            {
                _logPath = value;
            }
        }

        private string _fileName;
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get
            {
                return _fileName;
            }
            set
            {
                _fileName = value;
            }
        }


        /// <summary>
        /// 记录日志到文本中，每天记录一个日志
        /// </summary>
        /// <param name="message"></param>
        public void Write(string message)
        {
            try
            {
                string newFileName = ChangeFileName();
                if (newFileName != oldFileName)
                {
                    lock (syncRoot)
                    {
                        if (newFileName != oldFileName)
                        {
                            oldFileName = newFileName;
                            OpenFileStream(newFileName);
                        }
                    }
                }

                writer.WriteLine(message);
                TimeSpan s = DateTime.Now.Subtract(lastFlushTime);
                lastFlushTime = DateTime.Now;
                writer.Flush();


            }
            catch (Exception ex)
            {
                string str = ex.ToString();
                //忽略所有的错误
            }

        }
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="newFileName"></param>
        private void OpenFileStream(string newFileName)
        {
            if (fileStream != null)
            {
                writer.Flush();
                fileStream.Close();
            }
            fileStream = new FileStream(Path.Combine(LogPath, newFileName), FileMode.Append, FileAccess.Write);
            writer = new StreamWriter(fileStream, Encoding.Default);

        }

        /// <summary>
        /// 修改文件名，前面附带日期信息
        /// </summary>
        /// <returns></returns>
        private string ChangeFileName()
        {
            string curDate = DateTime.Now.ToString("yyyy-MM-dd_HH");
            return FileName + "_" + curDate + ".txt";
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            try
            {
                if (fileStream != null)
                {
                    fileStream.Flush();
                    fileStream.Close();
                    fileStream.Dispose();
                    IsOpenFile = false;
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}
